﻿#region 命名空间

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.SessionState;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;

#endregion

namespace WinnerCMS.WebUI.Admin
{
    /// <summary>
    ///     DataHandler 的摘要说明
    /// </summary>
    public class DataHandler : IHttpHandler, IReadOnlySessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Expires = 0;
            context.Response.CacheControl = "no-cache";
            context.Response.ContentType = "text/plain";

            var action = context.Request["Action"];
            byte modelId;
            byte.TryParse(context.Request["ModelId"], out modelId);


            if (action == "CheckTitle")
            {
                if (context.Request["Title"].Length == 0)
                {
                    context.Response.Write("{\"Msg\":\"请先输入标题。\"}");
                    return;
                }
                int i;
                if (new ArticleManage().Exists(context.Request["Title"], out i) == false)
                {
                    context.Response.Write("{\"Msg\":\"没有重名。\"}");
                }
            }

            else if (action == "updatePhotoListTitle")
            {
                var res = new PhotoListManage().UpdateTitle(Convert.ToInt32(context.Request["ID"]),
                    context.Request["Title"]);
                context.Response.ContentType = "application/json";
                context.Response.Write(SerializableHelper.SerializeToString(res, DataFormart.Json));
            }

            else if (action == "FilesUpload")
            {
                var fileUpload = context.Request.Files[0];
                var config = CMSConfig.Instance();
                var path = context.Server.UrlDecode(context.Request["dir"]);

                fileUpload.SaveAs(path + "\\" + fileUpload.FileName);
            }
            //else if (action == "SaveCMSSetting")
            //{
            //    var data = context.Request.Form["data"];
            //}
        }

        public bool IsReusable
        {
            get { return false; }
        }
    }
}